package com.wtx.fitness.controller;

import com.wtx.fitness.common.LoginDTO;
import com.wtx.fitness.common.LoginVO;
import com.wtx.fitness.common.Result;
import com.wtx.fitness.common.ResultCode;
import com.wtx.fitness.service.UserService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 登录控制器
 */
@RestController
@RequestMapping("/api")
public class LoginController {

  @Autowired
  private UserService userService;

  /**
   * 用户登录
   */
  @PostMapping("/login")
  public Result<LoginVO> login(@RequestBody @Valid LoginDTO loginDTO) {
    System.out.println("收到登录请求: " + loginDTO.getUsername() + ", 类型: " + loginDTO.getType());

    // 调用UserService的login方法进行登录
    LoginVO loginVO = userService.login(loginDTO);
    System.out.println("生成token: " + loginVO.getToken());

    return Result.success(loginVO);
  }
}